<?php
namespace app\index\controller;

use app\common\controller\Base;
use app\index\model\Logs as OperLogs;

/**
 * 操作日志
 */
class Logs extends Base
{

    //操作菜单ID
    private $opera_id;

    //自PHP5.4起
    private $operArray = [

        "1"  => ["name" => "发道具及邮件", "action" => ["index", "issue", "issue"]],

        "2"  => ["name" => "公告管理", "children" => ["20", "21", "22", "23"]],
        "20" => ["name" => '新增公告', "action" => ["index", "notice", "add"]],
        "21" => ["name" => '编辑公告', "action" => ["index", "notice", "update"]],
        "22" => ["name" => '删除公告', "action" => ["index", "notice", "delete"]],
        "23" => ["name" => '批量删除', "action" => ["index", "notice", "batDelete"]],

        "3"  => ["name" => "广播聊天", "children" => ["30", "31", "32", "33"]],
        "30" => ["name" => "广播新增", "action" => ["index", "Broadcast", "add"]],
        "31" => ["name" => "广播编辑", "action" => ["index", "Broadcast", "update"]],
        "32" => ["name" => "广播删除", "action" => ["index", "Broadcast", "delete"]],
        "33" => ["name" => "批量删除", "action" => ["index", "Broadcast", "batDelete"]],

        "4"  => ["name" => "封号禁言", "children" => ["40", "41", "42", "43"]],
        "40" => ["name" => "封号", "action" => ["index", "ban", "banAccount"]],
        "41" => ["name" => "解除封号", "action" => ["index", "ban", "unbanAccount"]],
        "42" => ["name" => "禁言", "action" => ["index", "ban", "banComment"]],
        "43" => ["name" => "解除禁言", "action" => ["index", "ban", "unbanComment"]],

        "5"  => ["name" => "白名单管理", "children" => ["50", "51", "52", "53"]],
        "50" => ["name" => "添加白名单", "action" => ["index", "whitelist", "add"]],
        "51" => ["name" => "编辑白名单", "action" => ["index", "whitelist", "update"]],
        "52" => ["name" => '删除白名单', "action" => ["index", "whitelist", "delete"]],
        "53" => ["name" => '批量删除', "action" => ["index", "whitelist", "batDelete"]],

        "6"  => ["name" => "服务器管理", "children" => ["60", "61", "62"]],
        "60" => ["name" => "关服", "action" => ["index", "server", "update"]],
        "61" => ["name" => "开服", "action" => ["index", "server", "update"]],
        "62" => ["name" => "停服", "action" => ["index", "server", "update"]],
        "63" => ["name" => "批量操作", "action" => ["index", "server", "batupdate"]],

        "7"  => ["name" => "礼包码管理", "children" => ["70", "71"]],
        "70" => ["name" => "添加礼包码", "action" => ["index", "issue", "issue"]],
        "71" => ["name" => "删除礼包码", "action" => ["index", "issue", "issue"]],
    ];
    /**
     * 控制器初始化
     * @return   void
     */
    public function _initialize()
    {
        //基类授权
        //
        parent::_initialize();
    }
    /**
     * 操作查询
     * @DateTime 2017-12-16
     * @return   string 操作id
     */
    private function getAction()
    {

        $opera          = $this->operArray;
        $this->opera_id = input('opera_id');
        return array_key_exists($this->opera_id, $this->operArray) ? $opera[$this->opera_id] : null;

    }
    /**
     * 操作日志查询
     * @DateTime 2017-12-16
     * @return   [type]
     */
    public function logs()
    {

        $map  = array(); //匹配条件
        $rows = array(); //匹配结果

        $action = $this->getAction();

        $this->name = $action['name'];

        //叶子菜单
        if (!empty($action) && array_key_exists("action", $action)) {

            //构造查询表达式

            $map['module']     = $action['action'][0];
            $map['controller'] = $action['action'][1];
            $map['action']     = $action['action'][2];

        } else if (!empty($action) && array_key_exists("children", $action)) {

            //带有子菜单的日志查询
            $operas = $this->operArray;
            $menus  = $action["children"];
            $maps   = [];

            //分拆子菜单
            foreach ($menus as $opera_id) {

                $match = [];
                $opera = array_key_exists($opera_id, $this->operArray) ? $operas[$opera_id] : null;

                if (!empty($opera)) {

                    $action = $opera['action'];

                    $match['module']     = $action[0];
                    $match['controller'] = $action[1];
                    $match['action']     = $action[2];

                    array_push($maps, $match);
                }
            }
        }

        //用户ID匹配
        $user = input('user');

        if (!empty($user)) {
            $map['staff_id'] = input('user');
        }

        //叶子菜单查询
        if (empty($maps)) {

            $status = $this->isServerOperation();

            if ('0' === $status or (!empty($status) && '3' != $status)) {
                $map['param'] = array('like', '%' . '"status":"' . $status . '"' . '%');
            }

            //执行查询,返回数据对象数组
            $result = OperLogs::all($map);

            foreach ($result as $data) {
                $item          = $data->getData();
                $item['_name'] = $this->name;
                array_push($rows, $item);
            }

            $total = count($rows);

            if ($total > 0) {
                $res['rows']  = $rows;
                $res['total'] = $total;
            } else {
                $res['total'] = 0;
                $res['rows']  = array();
            }
            _callback($res);

        } else {

            //类别查询
            //
            $res = []; //汇总记录

            foreach ($maps as $map) {

                $result = OperLogs::all($map);

                foreach ($result as $data) {
                    $item          = $data->getData();
                    $item['_name'] = $this->name;
                    array_push($rows, $item);
                }
                array_merge($res, $rows);
            }

            $total = count($res);

            if ($total > 0) {
                $rtn['rows']  = $res;
                $rtn['total'] = $total;
            } else {
                $rtn['total'] = 0;
                $rtn['rows']  = array();
            }
            _callback($rtn);
        }

    }
    /**
     * 操作人查询
     * @return   [type]
     */
    public function operators()
    {

        //构造闭包函数

        $closure = function ($query) {
            $query->field('distinct staff_id,staff_name')
                ->order('staff_id asc');
        };

        //执行查询,返回数据对象数组

        $result = OperLogs::all($closure);

        $rows = [];
        foreach ($result as $data) {
            $item = $data->getData();
            array_push($rows, $item);
        }

        //查询数据总计
        $total = count($rows);

        $res = array();

        if ($total > 0) {

            $res['rows']  = $rows;
            $res['total'] = $total;

        } else {

            $res['total'] = 0;
            $res['rows']  = array();

        }
        _callback($res);
    }
    /**
     * 开关服操作判断
     * @return   string|boolean 服务器操作状态码或服务器操作判断结果
     */
    public function isServerOperation()
    {

        $serverOpera = ['60', '61', '62', '63'];

        //返回状态码或服务器操作判断
        //
        if (in_array($this->opera_id, $serverOpera)) {

            switch ($this->opera_id) {
                case '60':
                    $status = '0';
                    break;
                case '61':
                    $status = '1';
                    break;
                case '62':
                    $status = '2';
                    break;
                case '63':
                    $status = '3';
                    break;
            }
            return $status;
        } else {
            return false;
        }

    }
}
